package com.masterclient.app.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.masterclient.app.R;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.baselibrary.utils.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * @auther hjian
 * @date 2020/3/11.
 */

public class PriceIntegralLayout extends LinearLayout {

    private Context context;
    private int priceTextColor = 0xFF141922;
    private float priceTextSize = ScreenUtils.dp2px(17),
            unitTextSize = ScreenUtils.dp2px(12),
            integralUnitSize = ScreenUtils.dp2px(12),
            unitPaddingBottom = ScreenUtils.dp2px(1.5f),
            integralUnitPaddingBottom = ScreenUtils.dp2px(1.5f);

    private TextView integralTv,integralUnitTv,addTv,priceUnitTv,priceTv;

    public PriceIntegralLayout(Context context) {
        this(context,null);
    }

    public PriceIntegralLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        setOrientation(HORIZONTAL);
        setGravity(Gravity.BOTTOM);

        TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.PriceIntegral);
        priceTextColor = mTypedArray.getColor(R.styleable.PriceIntegral_priceTextColor, 0xFF141922);
        priceTextSize = mTypedArray.getDimension(R.styleable.PriceIntegral_priceTextSize, ScreenUtils.dp2px(17));
        unitTextSize = mTypedArray.getDimension(R.styleable.PriceIntegral_unitTextSize, ScreenUtils.dp2px(12));
        integralUnitSize = mTypedArray.getDimension(R.styleable.PriceIntegral_integralUnitSize, ScreenUtils.dp2px(12));
        unitPaddingBottom = mTypedArray.getDimension(R.styleable.PriceIntegral_unitPaddingBottom, ScreenUtils.dp2px(1.5f));
        integralUnitPaddingBottom = mTypedArray.getDimension(R.styleable.PriceIntegral_integralUnitPaddingBottom, ScreenUtils.dp2px(3f));
        mTypedArray.recycle();

        integralTv = new TextView(context);
        integralTv.setTextColor(priceTextColor);
        integralTv.setTextSize(TypedValue.COMPLEX_UNIT_PX,priceTextSize);

        integralUnitTv = new TextView(context);
        integralUnitTv.setTextColor(priceTextColor);
        integralUnitTv.setGravity(Gravity.BOTTOM);
        integralUnitTv.setText("金币");
        integralUnitTv.setTextSize(TypedValue.COMPLEX_UNIT_PX,integralUnitSize);
        integralUnitTv.setPadding(ScreenUtils.dp2px(2),0,0,(int)integralUnitPaddingBottom);

        addTv = new TextView(context);
        addTv.setText("+");
        addTv.setTextColor(priceTextColor);
        addTv.setTextSize(TypedValue.COMPLEX_UNIT_PX,integralUnitSize);
        addTv.setPadding(0,0,ScreenUtils.dp2px(2),(int)integralUnitPaddingBottom - ScreenUtils.dp2px(0.7f));


        priceUnitTv = new TextView(context);
        priceUnitTv.setText("¥");
        priceUnitTv.setTextColor(priceTextColor);
        priceUnitTv.setTextSize(TypedValue.COMPLEX_UNIT_PX,unitTextSize);
        priceUnitTv.setPadding(0,0,ScreenUtils.dp2px(2),(int)unitPaddingBottom - ScreenUtils.dp2px(0.7f));
        priceUnitTv.setIncludeFontPadding(false);

        priceTv = new TextView(context);
        priceTv.setTextColor(priceTextColor);
        priceTv.setTextSize(TypedValue.COMPLEX_UNIT_PX,priceTextSize);
        priceTv.setIncludeFontPadding(false);

        Utils.setDin(integralTv,context);
        Utils.setDin(addTv,context);
        Utils.setDin(priceUnitTv,context);
        Utils.setDin(priceTv,context);

        addView(integralTv);
        addView(integralUnitTv);
        addView(addTv);
        addView(priceUnitTv);
        addView(priceTv);


    }

    public void setData(String integral, String money){
        integral = TextUtils.isEmpty(integral)?"0":integral;
        money = TextUtils.isEmpty(money)?"0":money;
        integralTv.setText(integral);
        priceTv.setText(money);
        integralTv.setVisibility(Long.parseLong(integral)>0?VISIBLE:GONE);
        integralUnitTv.setVisibility(Long.parseLong(integral)>0?VISIBLE:GONE);
        addTv.setVisibility(Long.parseLong(integral)>0&&Float.parseFloat(money)>0?VISIBLE:GONE);
        priceTv.setVisibility(Float.parseFloat(money)<=0&&Long.parseLong(integral)>0?GONE:VISIBLE);
        priceUnitTv.setVisibility(Float.parseFloat(money)<=0&&Long.parseLong(integral)>0?GONE:VISIBLE);
    }


    public void setData(String integral, String money, boolean moneyShow){
        integral = TextUtils.isEmpty(integral)?"0":integral;
        money = TextUtils.isEmpty(money)?"0":money;
        setData(integral,money);
        addTv.setVisibility(Long.parseLong(integral)>0?VISIBLE:GONE);
        priceTv.setVisibility(VISIBLE);
        priceUnitTv.setVisibility(VISIBLE);
    }


    public void floadSize(float textSize){
        Utils.setFloatSize(priceTv, priceTv.getText().toString(), textSize);
    }

    public void setPriceTextColor(int priceTextColor) {
        this.priceTextColor = priceTextColor;
        integralTv.setTextColor(priceTextColor);
        integralUnitTv.setTextColor(priceTextColor);
        addTv.setTextColor(priceTextColor);
        priceUnitTv.setTextColor(priceTextColor);
        priceTv.setTextColor(priceTextColor);
    }

    public void goneInView(){
        integralTv.setVisibility(GONE);
        integralUnitTv.setVisibility(GONE);
        addTv.setVisibility(GONE);
        priceUnitTv.setVisibility(GONE);
        priceTv.setVisibility(GONE);
    }


    private List<WarpLine> mWarpLineGroup;
    private int heightSize;

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int withMode = MeasureSpec.getMode(widthMeasureSpec);
        int withSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int with = 0;
        int height = 0;
        int childCount = getChildCount();
        measureChildren(widthMeasureSpec, heightMeasureSpec);

        switch (withMode) {
            case MeasureSpec.EXACTLY:
                with = withSize;
                break;
            case MeasureSpec.AT_MOST:
                for (int i = 0; i < childCount; i++) {
                    if (getChildAt(i).getVisibility() == VISIBLE)
                    with += getChildAt(i).getMeasuredWidth();
                }
                with += getPaddingLeft() + getPaddingRight();
                with = with > withSize ? withSize : with;
                break;
            case MeasureSpec.UNSPECIFIED:
                for (int i = 0; i < childCount; i++) {
                    if (getChildAt(i).getVisibility() == VISIBLE)
                    with += getChildAt(i).getMeasuredWidth();
                }
                with += getPaddingLeft() + getPaddingRight();
                break;
            default:
                with = withSize;
                break;

        }

        WarpLine warpLine = new WarpLine();
        mWarpLineGroup = new ArrayList<WarpLine>();
        for (int i = 0; i < childCount; i++) {
            if (getChildAt(i).getVisibility() == VISIBLE) {
                if (warpLine.lineWidth + getChildAt(i).getMeasuredWidth() /*+ mType.horizontal_Space*/ > with) {
                    if (warpLine.lineView.size() == 0) {
                        warpLine.addView(getChildAt(i));
                        mWarpLineGroup.add(warpLine);
                        warpLine = new WarpLine();
                    } else {
                        mWarpLineGroup.add(warpLine);
                        warpLine = new WarpLine();
                        warpLine.addView(getChildAt(i));
                    }
                } else {
                    warpLine.addView(getChildAt(i));
                }
            }
        }

        if (warpLine.lineView.size() > 0 && !mWarpLineGroup.contains(warpLine)) {
            mWarpLineGroup.add(warpLine);
        }
        height = getPaddingTop() + getPaddingBottom();
        for (int i = 0; i < mWarpLineGroup.size(); i++) {
            height += mWarpLineGroup.get(i).height;
        }
        switch (heightMode) {
            case MeasureSpec.EXACTLY:
                height = heightSize;
                break;
            case MeasureSpec.AT_MOST:
                height = height > heightSize ? heightSize : height;
                break;
            case MeasureSpec.UNSPECIFIED:
                break;
            default:
                break;
        }
        setMeasuredDimension(with, height);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        t = getPaddingTop();
        for (int i = 0; i < mWarpLineGroup.size(); i++) {
            int left = getPaddingLeft();
            WarpLine warpLine = mWarpLineGroup.get(i);
            int lastWidth = getMeasuredWidth() - warpLine.lineWidth;
            for (int j = 0; j < warpLine.lineView.size(); j++) {
                View view = warpLine.lineView.get(j);
                LinearLayout.LayoutParams layoutParams = (LayoutParams) view.getLayoutParams();
                view.layout(left, t + warpLine.height- view.getMeasuredHeight() , left + view.getMeasuredWidth(), t + warpLine.height);
                left += view.getMeasuredWidth();
            }
            t += warpLine.height;
        }
    }

    private final class WarpLine {
        private List<View> lineView = new ArrayList<View>();
        private int lineWidth = getPaddingLeft() + getPaddingRight();
        private int height = 0;
        private void addView(View view) {
            height = height > view.getMeasuredHeight() ? height : view.getMeasuredHeight();
            lineWidth += view.getMeasuredWidth();
            lineView.add(view);
        }
    }


}
